﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;

namespace BiliBiliCSharpStudy.old {
    /**
     * 自定义事件的简化声明 ; filed-like
     */
    class EventDemo06 {
        public static void Demo() {
            Test1();
        }

        public static void Test1() {
            Customer2 customer = new Customer2();
            Waiter2 waiter = new Waiter2();
            customer.Order += waiter.Action;
            customer.Action();
            customer.PayTheBill();
        }
    }

    public class OrderEventArgs2 : EventArgs {
        public string DishName { get; set; }
        public string Size { get; set; }
    }

    public delegate void OrderEventHandler2(Customer2 customer, OrderEventArgs2 e);

    public class Customer2 {
        // 区别只是这里： 直接声明一个字段，使用 `event` 修饰;
        public event OrderEventHandler2 Order;
        public double Bill { get; set; }

        public void PayTheBill() {
            Console.WriteLine("I will pay ${0}.", this.Bill);
        }

        public void WalkIn() {
            Console.WriteLine("Walk into the restaurant.");
        }

        public void SitDown() {
            Console.WriteLine("Sit down.");
        }
        public void Think() {
            for (int i = 0; i < 5; i++) {
                Console.WriteLine("Let me think ...");
                Thread.Sleep(1000);
            }

            if (this.Order != null) {
                OrderEventArgs2 e = new OrderEventArgs2();
                e.DishName = "kongpao Chicken";
                e.Size = "large";
                this.Order.Invoke(this, e);
            }
        }

        public void Action() {
            Console.ReadLine();
            this.WalkIn();
            this.SitDown();
            this.Think();
        }
    }

    public class Waiter2 {
        internal void Action(Customer2 customer, OrderEventArgs2 e) {
            //throw new NotImplementedException();
            Console.WriteLine("I will serve you the dish - {0}.", e.DishName);
            double price = 10;
            switch (e.Size) {
                case "small":
                    price = price * 0.5;
                    break;
                case "large":
                    price = price * 1.5;
                    break;
                default:
                    break;
            }
            customer.Bill += price;
        }
    }
}
